package cn.knight.basic.controller;

import cn.knight.basic.exception.BusinessException;
import cn.knight.basic.service.IVerifyCodeService;
import cn.knight.basic.utils.AjaxResult;
import cn.knight.basic.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
    * @Title: graph
    * @Description: 用户注册获取图形验证码
    * @Author: winnie
    * @Version: 1.0
    * @Date:  5/7/2022 下午6:03
    * @Parameters: [imageCodeKey]
    * @Return cn.knight.basic.utils.AjaxResult
    */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult graph(@PathVariable("imageCodeKey") String imageCodeKey){
        try {
            String base64Str = verifyCodeService.graph(imageCodeKey);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！！！");
        }
    }

    /**
    * @Title: SmsCode
    * @Description: 用户注册获取短信验证码
    * @Author: winnie
    * @Version: 1.0
    * @Date:  5/7/2022 下午6:13
    * @Parameters: [vo]
    * @Return cn.knight.basic.utils.AjaxResult
    */
    @PostMapping("/smsCode")
    public AjaxResult SmsCode(@RequestBody RegisterVo registerVo){
        try {
            verifyCodeService.smsCode(registerVo);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！！！");
        }
    }

    /**
    * @Title: SmsCode
    * @Description: 商家入驻短信验证码
    * @Author: winnie
    * @Version: 1.0
    * @Date:  8/7/2022 上午12:53
    * @Parameters: [vo]
    * @Return cn.knight.basic.utils.AjaxResult
    */
    @PostMapping("/settledsmsCode/{phone}")
    public AjaxResult settledsmsCode(@PathVariable String phone){
        try {
            verifyCodeService.settledsmsCode(phone);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！！！");
        }
    }
}
